package riteservices.services;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import riteservices.model.bean.Request;
import riteservices.model.bean.Round;
import riteservices.model.dao.RequestDAO;
import riteservices.util.Constantes;

public class RequestSrv {
 
	private RequestDAO requestDAO = new RequestDAO();
	 
	public String registerRequest(Request request) {
            request.setStatus(Constantes.PENDING);
            if(requestDAO.add(request)){
                return Constantes.TRUE_STR;
            }else{
                return Constantes.FALSE_STR;
            }
	}
        
        public Request getRequest(int requestId) {
            Request request = requestDAO.getRequestById(requestId);
            
            if(request != null){
                return request;
            }else{
                return null;
            }
	}
	 
	public String cancelRequest(int requestId) {
            
            Request request = requestDAO.getRequestById(requestId);
            Round round = request.getRound();
            request.setStatus(Constantes.CANCEL);
            round.getUsers().remove(request.getUserBySender());   
            request.setRound(round);
            RoundSrv roundSrv = new RoundSrv();
            roundSrv.updateRound(round);
            if(requestDAO.update(request)){
                return Constantes.TRUE_STR;
            }else{
                return Constantes.FALSE_STR;
            }
	}
        
        public boolean isPendiente(int requestId) {
            Request request = requestDAO.getRequestById(requestId);
            if(request.getStatus() == 0){
                return Boolean.TRUE;
            }
            else{
                return Boolean.FALSE;
            }
	}
	 
	public boolean isApproved(int requestId) {
            Request request = requestDAO.getRequestById(requestId);
            if(request.getStatus() == 1){
                return Boolean.TRUE;
            }
            else{
                return Boolean.FALSE;
            }
	}
        public boolean isReject(int requestId) {
            Request request = requestDAO.getRequestById(requestId);
            if(request.getStatus() == 2){
                return Boolean.TRUE;
            }
            else{
                return Boolean.FALSE;
            }
	}
        public boolean isCanceled(int requestId) {
            Request request = requestDAO.getRequestById(requestId);
            if(request.getStatus() == 3){
                return Boolean.TRUE;
            }
            else{
                return Boolean.FALSE;
            }
	}
        
	 
	public String approveRequest(int requestId) {
            Request request = requestDAO.getRequestById(requestId);
            Round round = request.getRound();
            request.setStatus(Constantes.APROVE);
            round.getUsers().add(request.getUserBySender());
            request.setRound(round);
            RoundSrv roundSrv = new RoundSrv();
            roundSrv.updateRound(round);
            return requestDAO.approveRequest(request);
	}
	 
	public String rejectRequest(int requestId) {
            Request request = requestDAO.getRequestById(requestId);
            request.setStatus(Constantes.REJECTED);
            return requestDAO.rejectRequest(request);
	}
        
        public List<Request> getRequestsUserReciver(int idUser, int idStatus){
            return requestDAO.getRequestsUserReciver(idUser, idStatus);
        }
        
        public List<Request> getRequestsUserSender(int idUser, int idStatus){
            return requestDAO.getRequestsUserSender(idUser, idStatus);
        }
        
        public void delete(Request request){
            requestDAO.delete(request);
        }
	 
}
 
